package com.alander.nexusgo.user.controller;

import com.alander.nexusgo.common.response.Result;
import com.alander.nexusgo.framework.web.starter.controller.BaseController;
import com.alander.nexusgo.user.model.constants.UserTagsEnum;
import com.alander.nexusgo.user.service.UserTagService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.getPageInfo;

/**
 * 用户标签管理
 */
@RestController
@RequestMapping("/api/user/tag")
@RequiredArgsConstructor
public class UserTagController extends BaseController {
    private final UserTagService userTagService;

    /**
     * 获取用户标签
     *
     * @param userId 用户id
     */
    @PostMapping("/listTag")
    public PageInfo<String> listTag(Long userId) {
        List<String> tags = userTagService.listUserTag(userId);
        return getPageInfo(tags);
    }

    /**
     * 设置标签
     *
     * @param userId 用户id
     * @param tag    标签
     */
    @PostMapping("/setTag")
    public Result<String> setTag(Long userId, String tag) {
        UserTagsEnum userTagsEnum = UserTagsEnum.valueOf(tag);
        boolean flag = userTagService.setTag(userId, userTagsEnum);
        if (!flag) {
            return fail("设置失败");
        }
        return success("设置成功");
    }

    /**
     * 取消标签
     *
     * @param userId 用户id
     * @param tag    标签
     */
    @PostMapping("/cancelTag")
    public Result<String> cancelTag(Long userId, String tag) {
        UserTagsEnum userTagsEnum = UserTagsEnum.valueOf(tag);
        if (!userTagService.cancelTag(userId, userTagsEnum)) {
            return fail("取消失败");
        }
        return success("设置成功");
    }

    /**
     * 是否包含某个标签
     *
     * @param userId 用户id
     * @param tag    标签
     */
    @PostMapping("/containTag")
    public Result<Boolean> containTag(Long userId, String tag) {
        UserTagsEnum userTagsEnum = UserTagsEnum.valueOf(tag);
        return success(userTagService.containTag(userId, userTagsEnum));
    }

}
